import got from "got";
import qs from "qs";
import { encryption } from "./encryption";

// import tunnel from "tunnel";
// const tunnelingAgent = tunnel.httpOverHttp({
//   proxy: {
//     host: "localhost",
//     port: 8888,
//   },
// });

// 获取初始 token 等全局变量
const authorization = process.env.ICAN_AUTHORIZATION;
const prefixUrl = process.env.ICAN_PREFIXURL;
const loginPath = process.env.ICAN_LOGIN_PATH;

const headers = {
  authorization,
};

const loginUrl = `${prefixUrl}${loginPath}`;
const refreshToken = async (refresh_token: string) => {
  const form = {
    refresh_token,
  };
  const formData = {
    ...form,
    grant_type: "refresh_token",
    scope: "server",
  };
  const params = qs.stringify(formData);
  const url = `${loginUrl}?${params}`;
  try {
    const res = await got.post(url, {
      method: "POST",
      headers,
      // agent: {
      //   http: tunnelingAgent,
      // },
    });
    return res;
  } catch (error) {
    console.error(error);
  }
};

export { refreshToken };
